<?php

	/**
	 * Defines a class to be used for Float Data Typing with PHP
	 * 
	 * @author Pablo Santiago Sánchez <phackwer@gmail.com>
	 * @copyright Copyright (c) 2008, Pablo Santiago Sánchez
	 * @license http://opensource.org/licenses/bsd-license.php BSD License
	 * @package pop
	 * @subpackage datatypes
	 */

	/**
	 * Class used to handle float numbers.
	 * 
	 * @package pop
	 * @subpackage datatypes
	 */
	class PFloat extends PTypeBase
	{
		/**
		 * Constructor, in which you must inform the default value.
		 * If none is informed, it uses 0.00 as default
		 * @param string $defaultval Default value. Must be a number
		 */
		public function __construct($defaultval = 0.00)
		{
			$this->__set("value", (float)$defaultval);
			$this->defaultval 	= $defaultval;
		}

		/**
		 * Magic method used to set the value and check if it's valid
		 * @param string $name name of the value
		 * @param string $value value itself
		 * @return bool 
		 */
		public function __set($name, $value)
		{
			if ($value == (string)(float)$value)
                $value = (float)$value;

			if ($value === "")
				$value = null;

			if ((!is_null($this->defaultval) && !is_null($value)) && !ctype_digit((string)str_replace(".","",$value)) && !is_float($value))
			{
				throw new Exception("This value must be a float or at least an string with a float value.\nValue: <font color=#FF0000>".$value."</font>");
			}
			else if ($name != "value")
			{
				throw new Exception("This attribute cannot be set");
			}
			else
			{
				if(is_null($this->defaultval) && is_null($value))
					$this->$name = null;
				else
					$this->$name = (float)$value;
				return true;
			}
		}
	}

?>